#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <string.h>

using namespace std;

void handler(int signum)
{
    //kill(getpid(), 2);
    sigset_t pending;
    cout<<endl;
    cout<<"received signal: "<<signum<<endl;
    while (1)
    {
        cout<<endl;
        sigpending(&pending);
        for (int signo = 1; signo < 32; signo++)
        {
            cout << sigismember(&pending, signo);
        }
        cout << endl;
        sleep(1);
    }
}

int main()
{
    struct sigaction act, oact;

    memset(&act, 0, sizeof(act));
    memset(&oact, 0, sizeof(oact));

    // 我们还想屏蔽1,3,4号信号
    sigemptyset(&act.sa_mask);  // 其实这步可以省略，因为上面我们已经初始化过act了
    sigaddset(&act.sa_mask,1);
    sigaddset(&act.sa_mask,3);
    sigaddset(&act.sa_mask,4);

    act.sa_handler = handler;
    sigaction(2, &act, &oact);

    while (1)
    {
        cout << "I am a process: " << getpid() << endl;
        sleep(1);
    }
    return 0;
}
